
function check_form(form, opts){
	if($(form).find('.error,.ext_error').length){
		$(opts.submit_btn).attr('disabled', 'disabled');
	}else{
		$(opts.submit_btn).removeAttr('disabled');
	}
}

var auto_check_opts = {
	'submit_btn' 	: '.save_btn',
	'next_focus'	: true,
	'onSuccess' 	: check_form
};

$(function(){
	$(".setting_form")
		.y_setting()
		.auto_check(auto_check_opts)
		.ajax_form({
			'submit_btn' 	: '.save_btn',
			'form_position'	: null,
			'success' 		: function(form, data){
				window.location.href = '/manage/Show3D/edit/' + data;
			}
		});

	//注册编辑器窗口自动适应窗口大小
	addWinResize(function(){
		$('.editor .edui-editor').width($('.setting_content').width() - 20);
	});

	$('.auto_img').auto_img();
	$('.y_image_ul .img_li').each(function(){
		init_img_li(this);
	});

	$('.cut_btn').click(function(){
		var url = $(this).attr('href');
		var process_bar = $('<div class="process_bar"><div class="process"></div><span></span></div>');
		var total = $('.y_image_ul>.img_li').length;
		cut_imgs(url, total, 0);
		$(this).hide().after(process_bar);
		return false;
	});
});

var error_count = 0;
function cut_imgs(url, total, item_index){
	$.get(url + '&index=' + item_index, function(data){
		var json = toJSON(data);
		if(json.result){
			++item_index
			var percent = item_index / total * 100;
			$('.process_bar span').html(percent.toFixed(2) + "%");
			$('.process_bar>.process').css({width : percent + "%"});
			error_count = 0;
			if(item_index == total){
				alert('切割完成！');
				return;
			}
			cut_imgs(url, total, item_index);
		}else{
			setTimeout(function(){
				++error_count;
				console.log('准备重试...error_count=' + error_count);
				cut_imgs(url, total, item_index);
			}, 1000);
		}
	}).error(function(){
		return;
		setTimeout(function(){
			++error_count;
			console.log('准备重试...error_count=' + error_count);
			cut_imgs(url, total, item_index);
		}, 1000);
	});
}
